package com.jhzn.geomap.controller;

import com.jhzn.geomap.model.Polygon;
import com.jhzn.geomap.service.PolygonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/polygons")
@CrossOrigin
public class PolygonController {
    private static final Logger logger = LoggerFactory.getLogger(PolygonController.class);

    private final PolygonService polygonService;

    public PolygonController(PolygonService polygonService) {
        this.polygonService = polygonService;
    }

    @PostMapping
    public Polygon createPolygon(@RequestBody Polygon polygon) {
        return polygonService.createPolygon(polygon);
    }

    @PutMapping("/{id}")
    public Polygon updatePolygon(@PathVariable Long id, @RequestBody Polygon polygon) {
        logger.info("Updating polygon with ID: {} and data: {}", id, polygon);
        polygon.setId(id);
        return polygonService.updatePolygon(polygon);
    }

    @GetMapping
    public List<Polygon> getAllPolygons() {
        logger.info("Fetching all polygons");
        List<Polygon> polygons = polygonService.getAllPolygons();

        logger.info("Fetched {} polygons", polygons);
        return polygons;
    }

    @DeleteMapping("/{id}")
    public void deletePolygon(@PathVariable Long id) {
        logger.info("Deleting polygon with ID: {}", id);
        polygonService.deletePolygon(id);
        logger.info("Deleted polygon with ID: {}", id);
    }
}